﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using System.IO;

namespace Feniks
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (frmMasura frm = new frmMasura())
            {
                frm.ShowDialog();
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (frmMakine frm = new frmMakine())
            {
                frm.ShowDialog();
            }
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (frmIsEmri frm = new frmIsEmri())
            {
                frm.ShowDialog();
            }
        }



        private void DizaynOlustur(byte tip)
        {
            string filename = Application.StartupPath + "\\etiket" + tip + ".prn";
            using (DataEntities ent = new DataEntities())
            {

                var sonuc = ent.Tasarimlar.Where(x => x.Tip == tip).FirstOrDefault();
                if (sonuc != null)
                {
                    File.WriteAllBytes(filename, sonuc.Data);
                }

                FastReport.Report rp = new FastReport.Report();
                if (File.Exists(filename))
                {
                    rp.Load(filename);
                }
                else
                {
                    BindingSource src = new BindingSource { DataSource = new Etiket() };
                    rp.FileName = filename;
                    rp.RegisterData(src, "Invoice", 10);
                    rp.GetDataSource("Invoice").Enabled = true;
                }
                rp.Prepare();
                rp.Design();
                if (File.Exists(rp.FileName))
                {
                    byte[] data = File.ReadAllBytes(rp.FileName);
                    if (sonuc != null)
                    {
                        sonuc.Data = data;
                    }
                    else
                    {
                        ent.AddToTasarimlar(new Tasarim() { Data = data, Tip = tip });
                    }
                    ent.SaveChanges();
                }
            }
        }

        private void DizaynYukle(byte tip)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filename = dlg.FileName;
                using (DataEntities ent = new DataEntities())
                {
                    var sonuc = ent.Tasarimlar.Where(x => x.Tip == tip).FirstOrDefault();
                    if (File.Exists(filename))
                    {
                        byte[] data = File.ReadAllBytes(filename);
                        if (sonuc != null)
                        {
                            sonuc.Data = data;
                        }
                        else
                        {
                            ent.AddToTasarimlar(new Tasarim() { Data = data, Tip = tip });
                        }
                        ent.SaveChanges();
                    }
                }
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SifreKontrol())
            {
                DizaynOlustur(0);
            }
        }

        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SifreKontrol())
            {
                DizaynOlustur(1);
            }
        }

        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SifreKontrol())
            {
                frmAyarlar frm = new frmAyarlar();
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Application.Restart();
                }
            }
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmEtiketBasimi frm = new frmEtiketBasimi();
            frm.ShowDialog();
        }

        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmKoliDara frm = new frmKoliDara();
            frm.ShowDialog();
        }

        private void barButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SifreKontrol())
            {
                DizaynYukle(1);
            }
        }

        private void barButtonItem12_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SifreKontrol())
            {
                DizaynYukle(0);
            }
        }

        private bool SifreKontrol()
        {
            bool sonuc = false;
            FrmSifre frm = new FrmSifre();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                sonuc = true;
            return sonuc;
        }

    }
}
